package administrator;

import java.awt.EventQueue;
public class SendMail {

	public JFrame frame;
	private JTextPane mailId;
	private JTextPane subject;
	private JTextPane body;
	private JTextField textField;
	private int selstud;
	
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					SendMail window = new SendMail();
					window.frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	
	public SendMail() {
		initialize();
	}

	
	private void initialize() {
		frame = new JFrame();
		frame.setBounds(350, 100, 550, 580);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.getContentPane().setLayout(null);
		
		JPanel panel = new JPanel();
		panel.setBackground(new Color(255, 69, 0));
		panel.setBounds(0, 24, 534, 56);
		frame.getContentPane().add(panel);
		panel.setLayout(null);
		
		JLabel lblNewLabel = new JLabel("E-Mail Notification to selected students");
		lblNewLabel.setFont(new Font("Tahoma", Font.BOLD, 14));
		lblNewLabel.setHorizontalAlignment(SwingConstants.CENTER);
		lblNewLabel.setBounds(10, 11, 514, 34);
		panel.add(lblNewLabel);
		
		mailId = new JTextPane();
		mailId.setBounds(202, 186, 322, 30);
		frame.getContentPane().add(mailId);
		
		JLabel lblEnterYourEmail = new JLabel("Enter Your E-Mail ID");
		lblEnterYourEmail.setFont(new Font("Tahoma", Font.PLAIN, 12));
		lblEnterYourEmail.setHorizontalAlignment(SwingConstants.CENTER);
		lblEnterYourEmail.setBounds(10, 186, 182, 30);
		frame.getContentPane().add(lblEnterYourEmail);
		
		JLabel lblEnterTheSubject = new JLabel("Enter the Subject");
		lblEnterTheSubject.setFont(new Font("Tahoma", Font.PLAIN, 12));
		lblEnterTheSubject.setHorizontalAlignment(SwingConstants.CENTER);
		lblEnterTheSubject.setBounds(10, 252, 172, 30);
		frame.getContentPane().add(lblEnterTheSubject);
		
		subject = new JTextPane();
		subject.setBounds(202, 252, 322, 30);
		frame.getContentPane().add(subject);
		
		JLabel lblEnterTheBody = new JLabel("Enter the body of the Mail");
		lblEnterTheBody.setFont(new Font("Tahoma", Font.PLAIN, 12));
		lblEnterTheBody.setHorizontalAlignment(SwingConstants.CENTER);
		lblEnterTheBody.setBounds(10, 321, 182, 20);
		frame.getContentPane().add(lblEnterTheBody);
		
		body = new JTextPane();
		body.setBounds(202, 321, 322, 130);
		frame.getContentPane().add(body);
		
		JButton btnSendMail = new JButton("Send Mail");
		btnSendMail.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSendMailActionPerformed(evt);
            }
        });
		btnSendMail.setBounds(216, 474, 89, 23);
		frame.getContentPane().add(btnSendMail);
		
		JButton btnCancel = new JButton("Cancel");
		btnCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelActionPerformed(evt);
            }
        });
		btnCancel.setBounds(216, 508, 89, 23);
		frame.getContentPane().add(btnCancel);
		
		textField = new JTextField();
		textField.setBounds(378, 125, 86, 20);
		frame.getContentPane().add(textField);
		textField.setColumns(10);
		
		JTextPane txtpnNoOfStudents = new JTextPane();
		txtpnNoOfStudents.setBackground(UIManager.getColor("Button.background"));
		txtpnNoOfStudents.setFont(new Font("Tahoma", Font.BOLD, 12));
		txtpnNoOfStudents.setForeground(new Color(0, 0, 0));
		txtpnNoOfStudents.setEditable(false);
		txtpnNoOfStudents.setText("No. of students to whom the mail is to be sent\r\n               (on the basis of merit list)");
		txtpnNoOfStudents.setBounds(21, 117, 310, 42);
		frame.getContentPane().add(txtpnNoOfStudents);
	}
	
	private void btnSendMailActionPerformed(java.awt.event.ActionEvent evt)
	{
		int rank = 0;
		String s = textField.getText();
		selstud = Integer.parseInt(s);
		
		String to = "";
		
		
		try
        {
            Class.forName("java.sql.Driver");
            
            Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3307/oats","root","anupam24");
            Statement stmt = con.createStatement();

            String sql="select * from selstud order by marks desc";
            
            ResultSet rs = stmt.executeQuery(sql);
            while(rs.next())
            {
                
                String email = rs.getString(2);
                rank = rank + 1;
                if(rank <= selstud)
                {
                   	 to = email + ","+ to;
                	 
                }
                System.out.println(to);

            }
            rs.close();
            stmt.close();
            con.close();
        }
        catch(Exception e)
        {
            System.out.println("Error is : "+e.getMessage());
        }

		
		
		String from = "anupamrocks24@gmail.com";
		
		String subject = "mail testing";
		String text = "testing how to send mail through java";
	
		/*Properties props = new Properties();
		props.put("mail.smtp.host", "smtp.gmail.com");
		props.put("mail.smtp.port", "465");
		
		Session mailSession = Session.getDefaultInstance(props);
		Message simpleMessage = new MimeMessage(mailSession);
		
		InternetAddress fromAddress = null;
		InternetAddress toAddress = null;
		try {
			fromAddress = new InternetAddress(from);
			toAddress = new InternetAddress(to);
		}
		catch (AddressException e) 
		{
			
			e.printStackTrace();
		}
		
		try
		{
			simpleMessage.setFrom(fromAddress);
			simpleMessage.setRecipient(RecipientType.TO, toAddress);
			simpleMessage.setSubject(subject);
			simpleMessage.setText(text);
			
			Transport.send(simpleMessage);
			System.out.println("HI SENT");
		} 
		catch (MessagingException e) 
		{
			e.printStackTrace();
		}*/		
	}
	private void btnCancelActionPerformed(java.awt.event.ActionEvent evt)
	{
		Admn_Menu am = new Admn_Menu();
		am.frame.setVisible(true);
		this.frame.setVisible(false);
	}
}
	